import { Window } from './Window';
import { ModelLoader } from '../model/ModelLoader';
import { ModelStore } from '../model/ModelStore';
import { View } from './View';

/**
 * Application class - The main entry point for the Mini-XAF application.
 * Manages application lifecycle, windows, and modules.
 */
export class Application {
  private static _instance: Application;
  private _windows: Window[] = [];
  private _activeWindow: Window | null = null;
  private _modelLoader: ModelLoader;
  private _isInitialized: boolean = false;

  /**
   * Get the singleton instance of the Application
   */
  public static getInstance(): Application {
    if (!Application._instance) {
      Application._instance = new Application();
    }
    return Application._instance;
  }
  private constructor() {
    // Initialize with mock data
    this._modelLoader = new ModelLoader(true);
  }

  /**
   * Get all application windows
   */
  // Removed duplicate 'windows' getter


  /**
   * Initialize the application
   * @param apiUrl The URL to the XAF WebAPI endpoint
   */
  public async initialize(apiUrl: string): Promise<void> {
    if (this._isInitialized) {
      console.warn('Application is already initialized');
      return;
    }

    try {
      // Load application model from the server
      await this._modelLoader.loadModel(apiUrl);
      
      // Create main window
      const mainWindow = new Window('main', 'Main Window');
      this._windows.push(mainWindow);
      this._activeWindow = mainWindow;
      
      this._isInitialized = true;
      console.log('Application initialized successfully');
    } catch (error) {
      console.error('Failed to initialize application:', error);
      throw error;
    }
  }

  /**
   * Get the active window
   */
  public get activeWindow(): Window | null {
    return this._activeWindow;
  }

  /**
   * Get all windows
   */
  public get windows(): Window[] {
    return [...this._windows];
  }

  /**
   * Get the model loader
   */
  public get modelLoader(): ModelLoader {
    return this._modelLoader;
  }

  /**
   * Check if the application is initialized
   */
  public get isInitialized(): boolean {
    return this._isInitialized;
  }

  /**
   * Create a new window
   * @param id Window identifier
   * @param title Window title
   */
  public createWindow(id: string, title: string): Window {
    const window = new Window(id, title);
    this._windows.push(window);
    return window;
  }

  /**
   * Set the active window
   * @param window Window to set as active
   */
  public setActiveWindow(window: Window): void {
    if (this._windows.includes(window)) {
      this._activeWindow = window;
    } else {
      throw new Error('Window is not registered with this application');
    }
  }

  /**
   * Close a window
   * @param window Window to close
   */
  public closeWindow(window: Window): void {
    const index = this._windows.indexOf(window);
    if (index >= 0) {
      this._windows.splice(index, 1);
      
      // If we closed the active window, set a new active window if possible
      if (this._activeWindow === window) {
        this._activeWindow = this._windows.length > 0 ? this._windows[0] : null;
      }
    }
  }
}
